

{% if criteo_pixels %}
    <script type="text/javascript" src="//static.criteo.net/js/ld/ld.js" async="true"></script>
    <script type = "text/javascript">
        

    window.doCriteoQ = function () {
        window.criteo_q = window.criteo_q || [];
        var deviceType = /iPad/.test(navigator.userAgent) ? "t" : /Mobile|iP(hone|od)|Android|BlackBerry|IEMobile|Silk/.test(navigator.userAgent) ? "m" : "d";
       
        window.criteo_q.push({ event: "setAccount", account: {{criteo_pixels | json}} });
        window.criteo_q.push({ event: "setSiteType", type: deviceType});
        {% if customerInfo.email %}
            window.criteo_q.push({ event: "setEmail", email:"{{customerInfo.email | string_sha256}}", hash_method: "sha256" });
        {% endif %}
        window.criteo_q.push(...arguments);
    };

    {%- if template == 'index' -%}
        doCriteoQ({event:'viewHome'});
    {%- elsif template == 'product_detail' -%}
        doCriteoQ({event:'viewItem',item:"{{product.spu  | escape}}"});
    {%- elsif template == 'search_detail' and search.keywords -%}
        var product_spu=[];
        {%- if search.products -%}
        {%- for product in search.products -%}
            product_spu.push("{{ product.spu  | escape }}");
        {%- endfor -%}
        {%- endif -%}
        doCriteoQ({event:'viewList', item:product_spu,keywords:'{{search.keywords}}'});

    {%- elsif template == 'cart_list' -%}
        let product_spu = oemcart_cart.items.map(item => {
            return {
                id: item.product.spu,
                price: item.price,
                quantity: item.quantity
            }
        });
        doCriteoQ({event: 'viewBasket',item: product_spu });
    {%- elsif template == 'collection_detail'  -%}
        var product_spu=[];
        {%- if collection.products -%}
        {%- for product in collection.products -%}
            product_spu.push("{{ product.spu }}");
        {%- endfor -%}
        {%- endif -%}
        doCriteoQ({event:'viewList', item: product_spu});

    {%- elsif controller == 'order' and action == 'success' -%}
        var products =[];
        {%- if order.products -%}
        {%- for product in order.products -%}
            products.push({id: "{{product.spu  | escape}}", price:"{{product.discount_price}}", quantity: "{{product.quantity}}"});
        {%- endfor -%}
        {%- endif -%}
        
        {%- if order.customer_id  -%}
            doCriteoQ({event:'trackTransaction', id:'{{order.order_number}}', item:products},{event: "setEmail",email:"{{order.customer_info.email}}"});
        {% else %}
            doCriteoQ({event:'trackTransaction', id:'{{order.order_number}}', item:products});
        {%- endif -%}
    {%- else -%}
        doCriteoQ({event:'viewPage'});
    {%- endif -%}


    
    function callback_criteo_add_to_cart(ecdata) {
        doCriteoQ({event:'addToCart', item:[{id: ecdata['ec_data_spu'], quantity: ecdata['ec_data_quantity'], price: ecdata['ec_data_price']}]});
    };
    function callback_criteo_generate_lead(ecdata) {};
    function callback_criteo_login(ecdata) {};
    function callback_criteo_begin_checkout(ecdata) {
    };
    function callback_criteo_share_product(ecdata) {};
    function callback_criteo_remove_from_cart(ecdata) {};
    function callback_criteo_sign_up(ecdata) {};
    function callback_criteo_add_address_information(ecdata) {};
    function callback_criteo_add_shipping_information(ecdata) {};
    function callback_criteo_add_payment_information(ecdata) {};
    function callback_criteo_to_wishlist(ecdata) {};
    </script>
{% endif %}
